/*
 * (C) Copyright 2001
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/*
 * Misc functions
 */
#include <common.h>
#include <command.h>
#include <asm/arch/cpu.h>
int do_sleep (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	ulong start = get_timer(0);
	ulong delay;

	if (argc != 2)
		return cmd_usage(cmdtp);

	delay = simple_strtoul(argv[1], NULL, 10) * CONFIG_SYS_HZ;

	while (get_timer(start) < delay) {
		if (ctrlc ())
			return (-1);

		udelay (100);
	}

	return 0;
}

U_BOOT_CMD(
	sleep ,    2,    1,     do_sleep,
	"delay execution for some time",
	"N\n"
	"    - delay execution for N seconds (N is _decimal_ !!!)"
);


#if defined(CONFIG_UT_OEM)

#if defined(CONFIG_S5P_LCD)

extern void LCD_setfgcolor(unsigned int color);
extern void LCD_setleftcolor(unsigned int color);
extern void LCD_setprogress(unsigned int color);
extern void LCD_turnon(void);
extern void LCD_backlight(int on);
extern void LCD_display_pic(int type);

#define CONFIG_TEST


int do_set_lcd_back_light(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int back_light_flg;

	u32 temp;
	
	if (argc != 2) 
	{
		printf ("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}
	back_light_flg = simple_strtoul(argv[1], NULL, 10);

#ifdef CONFIG_TEST
	printf("Enter Lcd test mode[%d] !\n", back_light_flg);
	switch(back_light_flg)
	{
		case 0:
			LCD_backlight(0);
			break;
			
		case 1:
			LCD_backlight(1);
			break;

		case 2:				
			LCD_setfgcolor(0x00000000);
			break;

		case 3:
			LCD_turnon();
			break;

		case 11:
			temp= *((volatile unsigned long *)CHIP_ID_BASE) ;	// & 0x300)
			printf("chipID=%08x \n", temp);
			break;

		case 4:
			LCD_display_pic(BATTERY_LOGO);
			break;
			
		case 5:
			LCD_display_pic(BATTERY_LP_LOGO);
			break;

		case 6:
			LCD_display_pic(RECOVERY_LOGO);
			break;
			
		case 7:	
			LCD_display_pic(UPDATING_LOGO);
			break;

		case 8:	
			LCD_display_pic(REV3_LOGO);
			break;

		case 9:	
			LCD_display_pic(REV4_LOGO);
			break;

		case 10:	
			LCD_display_pic(REV5_LOGO);
			break;
		
		default:
			break;
	}
#else	
	if (back_light_flg >1 ||back_light_flg<0) 
	{
		printf ("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}
	LCD_backlight(back_light_flg);	
       printf ("set lcd back light [%d]\n", back_light_flg);
#endif

	return 0;
}

U_BOOT_CMD(
	utsetbacklight, 2, 1,	do_set_lcd_back_light,
	"utsetbacklight   - utsetbacklight [x] x : 0[off] or 1[on]\n",
	"lcd backlight switch\n"
);


int do_ut_update_env(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
//	ut_update_lcd_param();
	LCD_turnon();
	return 0;
}

U_BOOT_CMD(
	utupdateenv, 1, 0,	do_ut_update_env,
	"utupdateenv - read environment and do changes immediately\n",
	NULL
);


#endif

extern void env_relocate_spec_movinand(void);
int do_refreshenv_env(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	env_relocate_spec_movinand();
	
	printf ("%s done !", __func__);
	return 0;
}

U_BOOT_CMD(
	refreshenv, 2, 1,	do_refreshenv_env,
	"reloadenv   - reload env value !\n",
);


extern void table_power_off(int wait);
int do_shutup(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int wait;

	printf ("%s enter !\n\n", __func__);

	if (argc > 2) {
		printf ("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}
	
	wait = simple_strtoul(argv[1], NULL, 10);
	
	table_power_off(wait);
	
	return 0;
}

U_BOOT_CMD(
	shut, 2, 1,	do_shutup,
	"reloadenv   - reload env value !\n",
);

extern void late_pmic_init(u8 volt);
int do_setddrV(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	int wait;

	printf ("%s enter !\n\n", __func__);

	if (argc != 2 ) {
		printf ("Usage:\n%s\n", cmdtp->usage);
		return 1;
	}
	
	wait = simple_strtoul(argv[1], NULL, 10);
	
	late_pmic_init(wait);
	
	return 0;
}

U_BOOT_CMD(
	setddrV, 2, 1,	do_setddrV,
	"setddrV   - set ddr volt !\n",
	"setddrV 1=1.2v, 2=1.25v, 3=1.3v, 4=1.35v, 5=1.4v, 6=1.45v, 7=1.5v\n"
);
#endif


